package cn.itcast.servlet;

import cn.itcast.service.impl.TownServiceImpl;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/PageQueryTownServlet")
public class PageQueryTownServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        try {
            //获取表单数据
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("pageSize");
            if (StringUtils.isEmpty(pageNum)){
                pageNum="1";
            }
            if (StringUtils.isEmpty(pageSize)){
                pageSize="10";
            }

            //调用Servlet
            PageInfo pageInfo = new TownServiceImpl().pageQuery(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            //把Java对象变成JSON格式的字符串

            //把数据写个浏览器
            response.getWriter().println(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}